package psi.springapp.mvc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import model.Party;
import model.User;
import psi.springapp.mvc.repositories.UserRepository;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IDEA.
 * User: Ѱ
 * Date: 05.10.14
 * Time: 10:29
 */
@Service
public class UserService {
    @Autowired
    UserRepository userRepository;

    public UserService() {
    }

    public void saveUser(User user) {
        userRepository.save(user);

    }

    public void voteParty(User user, Party party) {
        if (!user.isVoted()) {
            party.setVotesNumb(party.getVotesNumb() + 1);
            user.setIdPartyVoted(party.getId_party());
            user.setVoted(true);
        }

    }

    public Iterable<User> userAll() {
        return userRepository.findAll();
    }

    public void deleteAll() {
        userRepository.deleteAll();
    }

    public List<User> userList() {
        List<User> users = new LinkedList<User>();
        Iterator i = userAll().iterator();
        User tmp;
        while (i.hasNext()) {
            tmp = (User) i.next();
            users.add(tmp);
        }
        return users;
    }

    public User getById(int id) {
        return userRepository.findOne(id);
    }


}
